<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 */

defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'choujiang';
//exi('xxxx','error');
$return=array();
//查询当前粉丝是否关注当前公众号
$fans = cfc('core')->get_fans($_SESSION['openid']);
$fansinfo = pdo_get('core_fans',array('id'=>$fans['id'],'uniacid'=>$_SESSION['uniacid']));
$follow = $fansinfo['follow'];
if($_RQ['op']=='choujiang'){
	file_put_contents(ROOT_D.'/cssssss.txt','fff');
	if($_RQ['rid']){
		$id=intval($_RQ['rid']);
	}
	if(!$id){
		exi('参数错误！','error');
	}
	//查询对应活动的参数
	$item=pdo_get('jy_h5game_reply',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	//获取当前时间
	$time = time();	
	
	//实际参与该活动的人数
	$playerNum = pdo_count('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid']));
	//总人数=实际人数+虚拟人数
	$playerNums = $playerNum + $item['virtualRegNum'];
	 
	//活动奖项数据 转成数组格式
	$award = json_decode($item['award'],true);
	//活动奖项数量
	$awardNum = count($award);
	//奖品总数量
	for($x=0; $x<$awardNum; $x++){
		$allPrize += $award[$x]['prizeNum'];
	} 
    //按排名取与奖品数量等同的玩家(被拉黑玩家不在计量范围)	
    if($allPrize){
		$list = pdo_getall('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'shielding'=>!2),'','','bestScore desc limit 0,'.$allPrize);
    }
	//排行榜
	if($item['isRank'] == 2){
		$rankList = pdo_getall('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'shielding'=>!2),'','','bestScore desc limit 0,'.$item['rank']);
	}
	//该活动玩家积分排名(前30)
	else{
		$rankList = pdo_getall('jy_h5game_fans',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'shielding'=>!2),'','','bestScore desc limit 0,30');
	}
	
	//每个奖项奖品数量
	//for ($x=0; $x<$awardNum; $x++){
	//	$prize[$x] = $award[$x]['prizeNum'];
   // } 
	//$return['html']='../'.ADDONS.'/jy_h5game/custom/templates/mobile/2048/index.html';
	if($list){
		$paiming = 1;
		foreach($list as &$row){
			$row['paiming']=$paiming;
			$paiming = $paiming+1;	
		}
	}
	if($rankList){
		$paiming = 1;
		foreach($rankList as &$row){
			$row['paiming']=$paiming;
			$paiming = $paiming+1;
		}
	}
    //当前玩家信息
	$play = pdo_get('jy_h5game_fans',array('fansId'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid']));
	//当前玩家得分
	$score = pdo_getcolumn('jy_h5game_fans',array('fansId'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id),'bestScore');
	$return['award'] = array('action'=>'award','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$return['notice'] = array('action'=>'notice','query'=>'id='.$id,'plugin'=>'jy_h5game');
	$return['info'] = array('action'=>'info','query'=>'id='.$id,'plugin'=>'jy_h5game');
	if($item['gameTypeSetting'] == 1){
		//查询玩家中奖次数
		$awardTimes = pdo_count('jy_h5game_award',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'fansID'=>$play['fansID']));
		file_put_contents(ROOT_D.'/cssssss.txt',$awardTimes);
		//Z玩家在该活动中的信息
		$isInList = pdo_get('jy_h5game_fans',array('fansID'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id));
		//exi($item['playTotalLimit']>0&&$isInList['totalnum'] >= $item['playTotalLimit'],'error');
		if(time() <= strtotime($item['starttime']) ||$item['status'] == 1){
			$return['errorCode'] = 10998;
			$errorCode = 10998;
			$return['errorMsg'] = '亲，本次抽奖活动将于'.$item['starttime'].'开启,敬请期待！';
		}
		elseif(time() >= strtotime($item['endtime']) || $item['status'] == 3) {
			$return['errorCode'] = 10998;
			$errorCode = 10998;
			$return['errorMsg'] = '亲，本次抽奖活动已结束，请关注我们的下一次活动，谢谢～';
		}
		//每人中奖次数限制
		elseif($item['awardTimesLimit']>0 && $item['awardTimesLimit'] <= $awardTimes ){
			$return['errorCode'] = 10998;
			$errorCode = 10998;
			$return['errorMsg'] = '亲，本次抽奖活动最多允许中奖'.$item['awardTimesLimit'].'次，您已经中奖'.$awardTimes.'次';
		}
		//抽奖次数限制
		elseif($item['playTotalLimit']>0&&$isInList['totalnum'] >= $item['playTotalLimit']){
			$return['errorCode'] = 10998;
			$errorCode = 10998;
			$return['errorMsg'] = '亲，本次抽奖活动最多允许参加'.$item['playTotalLimit'].'次，您已经参加'.$isInList['totalnum'].'次'; 
		}
		else{
		/*	$rate = $item['winningRate'];
			$arr['p1'] = round(100 * $rate * $prize['0']/$allPrize);
			$arr['p2'] = round(100 * $rate * $prize['1']/$allPrize);
			$arr['p3'] = round(100 * $rate * $prize['2']/$allPrize);
			$arr['p4'] = round(100 * $rate * $prize['3']/$allPrize);
			$arr['p5'] = round(100 * $rate * $prize['4']/$allPrize);
			$arr['p6'] = round(100 * $rate * $prize['5']/$allPrize);
			$arr['p7'] = round(100 * $rate * $prize['6']/$allPrize);
			*/
			$arr['p1'] = 100 * $award['0']['rate'];
			$arr['p2'] = 100 * $award['1']['rate'];
			$arr['p3'] = 100 * $award['2']['rate'];
			$arr['p4'] = 100 * $award['3']['rate'];
			$arr['p5'] = 100 * $award['4']['rate'];
			$arr['p6'] = 100 * $award['5']['rate'];
			$arr['p7'] = 100 * $award['6']['rate'];
			//print_r($arr['p1'].'gg');exit;
		
			$arr['p8'] = 10000 - $arr['p1'] - $arr['p2']- $arr['p3']- $arr['p4']- $arr['p5']- $arr['p6']- $arr['p7'];
				
			$result = $this->get_rand($arr);
			file_put_contents(ROOT_D.'/cssssss.txt',$result);
			//查询中奖数量
			if($result == 'p8'){
				$errorCode = 1;	 
			}
			else{
				$code1 = $this->make_coupon_card();	
				if ($result == 'p1') {
					//1等奖已中奖数量
					$awardnum = pdo_count('jy_h5game_award',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'awardLevel' => '1'));
					if($awardnum>=$award['0']['prizeNum']){
						$result = 'p2';  
					}
					else{
						$errorCode = 0;
						$return['prize'] = 1;
						$return['status'] = 1;
						$awarddata = array(
							'rid' => $id,
							'fansID' => $play['fansID'],
							'award' => $award['0']['prizeName'],
							'awardinfo'=>$play['name'],
							'uniacid'=>$_SESSION['uniacid'],
							'awardLevel' => '1',
							'createtime' => time(),
							'ausername'=>$play['ausername'],
							'address'=>$play['address'],
							'aphone'=>$play['tel'],
							'code1'=>$code1,
							'awardstatus'=>'1',
							'awardTime'=>time()
						);
									//中奖后，存入数据
						$this->insert('award',$awarddata);	
						if($award['0']['prizeType'] == '2'){
						//查询会员积分
							$credit1 = pdo_getcolumn('core_members',array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']),'credit1');
							$newcredit1 = $credit1+$award['0']['jifen'];
							 pdo_update('core_members',array('credit1'=>$newcredit1),array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']));
						}
					}
				   }
                if ($result == 'p2') {
				    //2等奖已中奖数量
					$awardnum = pdo_count('jy_h5game_award',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'awardLevel' => '2'));
				    if($awardnum>=$award['1']['prizeNum']){
					   $result = 'p3';  
				    }
				    else{
				    $errorCode = 0;
					$return['prize'] = 2;
                    $return['status'] = 1;
					$awarddata = array(
						'rid' => $id,
						'fansID' => $play['fansID'],
						'award' => $award['1']['prizeName'],
						'awardinfo'=>$play['name'],
						'uniacid'=>$_SESSION['uniacid'],
						'awardLevel' => '2',
						'createtime' => time(),
						'ausername'=>$play['ausername'],
						'address'=>$play['address'],
		                'aphone'=>$play['tel'],
						'code1'=>$code1,
						'awardstatus'=>'1',
		                'awardTime'=>time()
					);
					//中奖后，存入数据
                    $this->insert('award',$awarddata);	
					if($award['1']['prizeType'] == '2'){
						//查询会员积分
						$credit1 = pdo_getcolumn('core_members',array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']),'credit1');
						$newcredit1 = $credit1+$award['1']['jifen'];
						pdo_update('core_members',array('credit1'=>$newcredit1),array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']));
					}
					}
			    }
                if($result == 'p3'){
					//3等奖已中奖数量
				    $awardnum = pdo_count('jy_h5game_award',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'awardLevel' => '3'));
				    if($awardnum>=$award['2']['prizeNum']||empty($award['2'])){
					   $result = 'p4';
				    }
				    else{
						$errorCode = 0;
					    $return['prize'] = 3;
                        $return['status'] = 1;
					    $awarddata = array(
							'rid' => $id,
							'fansID' => $play['fansID'],
							'award' => $award['2']['prizeName'],
							'awardinfo'=>$play['name'],
							'uniacid'=>$_SESSION['uniacid'],
							'awardLevel' => '3',
							'createtime' => time(),
							'ausername'=>$play['ausername'],
							'address'=>$play['address'],
		                    'aphone'=>$play['tel'],
							'code1'=>$code1,
							'awardstatus'=>'1',
		                    'awardTime'=>time()
						);
					    //中奖后，存入数据
                        $this->insert('award',$awarddata);	
						if($award['2']['prizeType'] == '2'){
							//查询会员积分
							$credit1 = pdo_getcolumn('core_members',array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']),'credit1');
							$newcredit1 = $credit1+$award['2']['jifen'];
							pdo_update('core_members',array('credit1'=>$newcredit1),array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']));
							}
						}	
				}
                if ($result == 'p4'){
					//4等奖已中奖数量
				    $awardnum = pdo_count('jy_h5game_award',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'awardLevel' => '4'));
				    if($awardnum>=$award['3']['prizeNum']||empty($award['3'])){
					   $result = 'p5';
					 
					   
				    }
				   else{
						$errorCode = 0;
					    $return['prize'] = 4;
                        $return['status'] = 1;
						$awarddata = array(
							'rid' => $id,
							'fansID' => $play['fansID'],
							'award' => $award['3']['prizeName'],
							'awardinfo'=>$play['name'],
							'uniacid'=>$_SESSION['uniacid'],
							'awardLevel' => '4',
							'createtime' => time(),
							'ausername'=>$play['ausername'],
							'address'=>$play['address'],
		                    'aphone'=>$play['tel'],
							'awardstatus'=>'1',
							'code1'=>$code1,
		                    'awardTime'=>time()
							);
						//中奖后，存入数据
                        $this->insert('award',$awarddata);	
						if($award['3']['prizeType'] == '2'){
							//查询会员积分
							$credit1 = pdo_getcolumn('core_members',array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']),'credit1');
							$newcredit1 = $credit1+$award['3']['jifen'];
							pdo_update('core_members',array('credit1'=>$newcredit1),array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']));
						}
					}	
				}
				 if ($result == 'p5') {
					   //5等奖已中奖数量
				   $awardnum = pdo_count('jy_h5game_award',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'awardLevel' => '5'));
				 if($awardnum>=$award['4']['prizeNum']||empty($award['4']))
				   {
					   $result = 'p6';
					    
					   
				   }
				   else{
					   $errorCode = 0;
					    $return['prize'] = 5;
                        $return['status'] = 1;
					$awarddata = array(
								    'rid' => $id,
									'fansID' => $play['fansID'],
									'award' => $award['3']['prizeName'],
									'awardinfo'=>$play['name'],
									'uniacid'=>$_SESSION['uniacid'],
									'awardLevel' => '5',
									'createtime' => time(),
									'ausername'=>$play['ausername'],
									'address'=>$play['address'],
		                            'aphone'=>$play['tel'],
									'code1'=>$code1,
									'awardstatus'=>'1',
		                            'awardTime'=>time()
									);
								//中奖后，存入数据
                             $this->insert('award',$awarddata);	
							 if($award['4']['prizeType'] == '2'){
								//查询会员积分
								 $credit1 = pdo_getcolumn('core_members',array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']),'credit1');
							     $newcredit1 = $credit1+$award['4']['jifen'];
								 pdo_update('core_members',array('credit1'=>$newcredit1),array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']));
							}
							}	
				 }if ($result == 'p6') {
					   //6等奖已中奖数量
				   $awardnum = pdo_count('jy_h5game_award',array('rid'=>$id,,'uniacid'=>$_SESSION['uniacid'],'awardLevel' => '6'));
				 if($awardnum>=$award['5']['prizeNum']||empty($award['5']))
				   {
					   $result = 'p7';
					   
					   
				   }
				   else{
					   $errorCode = 0;
					    $return['prize'] = 6;
                        $return['status'] = 1;
					$awarddata = array(
								    'rid' => $id,
									'fansID' => $play['fansID'],
									'award' => $award['3']['prizeName'],
									'awardinfo'=>$play['name'],
									'uniacid'=>$_SESSION['uniacid'],
									'awardLevel' => '6',
									'createtime' => time(),
									'ausername'=>$play['ausername'],
									'address'=>$play['address'],
		                            'aphone'=>$play['tel'],
									'code1'=>$code1,
									'awardstatus'=>'1',
		                            'awardTime'=>time()
									);
								//中奖后，存入数据
                             $this->insert('award',$awarddata);	
							if($award['5']['prizeType'] == '2'){
								//查询会员积分
								 $credit1 = pdo_getcolumn('core_members',array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']),'credit1');
							     $newcredit1 = $credit1+$award['5']['jifen'];
								 pdo_update('core_members',array('credit1'=>$newcredit1),array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']));
							}
							}	
				 }if ($result == 'p7') {
					   //7等奖已中奖数量
				   $awardnum = pdo_count('jy_h5game_award',array('rid'=>$id,'uniacid'=>$_SESSION['uniacid'],'awardLevel' => '7'));
				 if($awardnum>=$award['6']['prizeNum']||empty($award['6']))
				   {
					   $result = 'p8';
					    $errorCode = 1;
						
					   
				   }
				   else{
					   $errorCode = 0;
					    $return['prize'] = 7;
                        $return['status'] = 1;
					$awarddata = array(
								    'rid' => $id,
									'fansID' => $play['fansID'],
									'award' => $award['3']['prizeName'],
									'awardinfo'=>$play['name'],
									'uniacid'=>$_SESSION['uniacid'],
									'awardLevel' => '7',
									'createtime' => time(),
									'ausername'=>$play['ausername'],
									'address'=>$play['address'],
		                            'aphone'=>$play['tel'],
									'code1'=>$code1,
									'awardstatus'=>'1',
		                            'awardTime'=>time()
									);
								//中奖后，存入数据
                             $this->insert('award',$awarddata);	
							 if($award['6']['prizeType'] == '2'){
								//查询会员积分
								 $credit1 = pdo_getcolumn('core_members',array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']),'credit1');
							     $newcredit1 = $credit1+$award['6']['jifen'];
								 pdo_update('core_members',array('credit1'=>$newcredit1),array('id'=>$play['fansID'],'uniacid'=>$_SESSION['uniacid']));
							}
							}	
				 }
							
							 
							    $title = '真实物品券';
								$type = '真实物品券';
								$value = 1;
							$award2 = array(
								'title' => $awarddata['award'],
								'value' => '22',
								'type' => '1',
								'remsg' => !empty($remsg) ? $remsg : ' ',
								'point' => '2',
								'point_name' => '11',
								'give_point' => '1',
								'give_name' => '444444',
								'level' => '4',
								'detail_url' => 'xxxx'
								);
			
	             $return['errorCode'] = $errorCode;
	             $return['msg'] = '2222222222';
	              $return['data'] = $award2;
				   
				 
		
	}   
         //记录抽奖次数
                 if($item['isLimitDrawTimes'] == 1){ 
				 
		        $totalnum = pdo_getcolumn('jy_h5game_fans',array('fansId'=>$fansinfo['mid'],'uniacid'=>$_SESSION['uniacid'],'rid'=>$id),'totalnum');			
				$totalnum = $totalnum+1;
                pdo_update('jy_h5game_fans',array('totalnum'=>$totalnum),array('fansID' => $play['fansID'],'rid'=>$id,'uniacid'=>$_SESSION['uniacid']));		 
				
				 }
				 }
	}
	
	$return['result'] = $result;
	$return['code'] = $errorCode;
	$return['item']=$item;
	$return['play']=$play;
	$return['fansinfo']=$fansinfo;
	$return['playerNums']=$playerNums;
	$return['list']=$list;
	$return['score']=$score;
	$return['rankList']=$rankList;
	$return['awarddata']=$awarddata;
	$return['success']=$errorCode;
	$return['awardlist']=$award;
	

   //$return=json_encode($return);
  // $return['prize'] = 1;
  // $return['status'] = 1;
 //exi($return['success'],'error');
 // exi($result,'error');
	exi($return);
}

		
		


